/**
 * 时间戳转换成yyyy-mm-dd hh:mm:ss
 * @param {String} time
 * @returns
 */
const timestampToTime = time => {
  const date = new Date(time * 1)

  const y = date.getFullYear()
  const m = (date.getMonth() + 1 + '').padStart(2, '0')
  const d = (date.getDate() + '').padStart(2, '0')

  const hh = (date.getHours() + '').padStart(2, '0')
  const mm = (date.getMinutes() + '').padStart(2, '0')
  const ss = (date.getSeconds() + '').padStart(2, '0')

  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}

/**
 *
 * @param {Date} time
 * @returns
 */
const timeToDate = time => {
  // 分
  // hour 60
  // day 1440
  // week 10080
  // month 44640
  // year 525600
  const nowDate = new Date().getTime()
  const disDate = nowDate - time
  const resDate = disDate / (60 * 1000)
  let res = ''
  let unit = ''
  if (resDate < 60) {
    res = Math.floor(resDate)
    unit = '分钟'
  } else if (resDate >= 60 && resDate < 1440) {
    res = Math.floor(resDate / 60)
    unit = '小时'
  } else if (resDate >= 1440 && resDate < 10080) {
    res = Math.floor(resDate / 1440)
    unit = '天'
  } else if (resDate >= 10080 && resDate < 44640) {
    res = Math.floor(resDate / 10080)
    unit = '周'
  } else if (resDate >= 44640 && resDate < 525600) {
    res = Math.floor(resDate / 44640)
    unit = '月'
  } else if (resDate >= 525600) {
    res = Math.floor(resDate / 525600)
    unit = '年'
  }

  return `${res}${unit}前`
}

export default {
  timestampToTime,
  timeToDate
}
